package com.lcxw.hxzbapi.controller.friendcirclecontroller;

import com.lcxw.hxzbapi.controller.membercontroller.MemberRealization;
import com.lcxw.hxzbapi.entity.requestBean.FriendCircleResourceUpload;
import com.lcxw.hxzbapi.entity.requestBean.GetFriendInfoReq;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.ObsUtil;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("api/friendCircle")
@Slf4j
public class FriendCircleController {

    private ObsUtil obsUtil = new ObsUtil();

    @Resource
    MemberRealization memberRealization;

    /**
     * 获取朋友的信息接口
     */
    @PostMapping("/friendInfo/get")
    public Response getFriendInfo(@RequestBody GetFriendInfoReq getFriendInfoReq){

        long friendId = Long.parseLong(getFriendInfoReq.getFriendId());

        Map<String, Object> userInfo;

        try {
            userInfo = memberRealization.getFriendInfo(friendId);
        } catch (Exception e) {
            return Response.dbFail(log,"get friend info");
        }

        return Response.ok(userInfo);

    }

    /**
     * 朋友圈动态资源上传，即图片、视频等上传接口
     */
    @PostMapping("/resource/upload")
    public Response friendCircleResourceUpload(@RequestBody FriendCircleResourceUpload requestBody){

        String userId = requestBody.getUserId();

        String resourceName = requestBody.getResourceName();
        String resource = requestBody.getResource();

        resourceName = "friendCircle/"+ userId +  resourceName;
        String url;

        try {
            url = obsUtil.uploadPic(resourceName, resource);
        } catch (Exception e) {
            return Response.error(6001,"上传文件失败，请确认文件大小或网络是否异常。");
        }

        return Response.ok(url);

    }

}
